<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="headerDiv">
        <img src="../assets/xiaoxin.jpg" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="out">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->

      <el-aside :width="isCollapse ? '65px' : '200px'">
        <div class="toggle-button" @click="toggleCollpase">|||</div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#9dd3e8"
          text-color="#3b33b"
          active-text-color="#539ded"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="path"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savaNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      path: ''
    }
  },
  created() {
    this.getMenuList()
    this.path = window.sessionStorage.setItem('path', 'path')
  },
  methods: {
    toggleCollpase() {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menulist = res.data
      }
    },
    out() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //保存链接的激活状态
    savaNavState(path) {
      window.sessionStorage.setItem('path', path)
      this.path = path
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #9dd3e8;
  display: flex;

  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #c4751c;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #9dd3e8;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-container {
  height: 100%;
}
.el-button {
  width: 60px;
  height: 60px;
  border-radius: 5px;
  background-color: #945b41;
}
.iconfont {
  margin-right: 15px;
  font-size: 26px;
}
.toggle-button {
  background-color: #945b41;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.headerDiv {
  img {
    width: 50px;
    height: 50px;
  }
}
</style>
